﻿using RestSharp;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WpfApp1.Models;
using WpfApp1.Services;

namespace WpfApp1.Service
{
    public class LoginUserService: ILoginUserService
    {

        private  HttpRestClient client;
        public LoginUserService(HttpRestClient client)
        {
            this.client = client;
        }
        public async Task<ApiResponse<string>> UserReg(UserRegDto userReg)
        {
          
            BaseRequest request = new BaseRequest();
            request.Method = RestSharp.Method.Post;
            request.Route = "/user/register";
            request.Parameter = userReg;           
            return await client.ExecuteAsync<string>(request);
        }
        public async Task<ApiResponse<string>> SendCode(string phone)
        {
          
            BaseRequest request = new BaseRequest();
            request.Method = RestSharp.Method.Post;
            request.ContentType = "application/x-www-form-urlencoded";
            request.Route = $"/user/sendsms";       
            var parameters =new List<Parameter>() {
             Parameter.CreateParameter("phone", phone,ParameterType.GetOrPost)
            };              


            return await client.ExecuteAsyncParms<string>(request, parameters: parameters);
            


        }
        public async Task<ApiResponse<UserInfo>> Login(LoginDto userReg)
        {

            BaseRequest request = new BaseRequest();
            request.Method = RestSharp.Method.Post;
            request.ContentType = "application/x-www-form-urlencoded";
            request.Route = "/user/login";           
            var parameters = new List<Parameter>() {
             Parameter.CreateParameter("userName", userReg.UserName,ParameterType.GetOrPost)
             ,
              Parameter.CreateParameter("password", userReg.Password,ParameterType.GetOrPost)
            };
            return await client.ExecuteAsyncParms<UserInfo>(request, parameters: parameters);
        }
    }
    public class LoginDto
    {
        public string UserName { get; set; }       
        public string Password { get; set; }
    }
    public class UserRegDto
    {
        public string Phone { get; set; }
        public string Code { get; set; }
        public string password { get; set; }
    }
}
